package cn.wolfcode.luowowo.article.service.impl;

import cn.wolfcode.luowowo.article.domain.Travel;
import cn.wolfcode.luowowo.article.domain.TravelContent;
import cn.wolfcode.luowowo.article.mapper.TravelContentMapper;
import cn.wolfcode.luowowo.article.mapper.TravelMapper;
import cn.wolfcode.luowowo.article.service.ITravelService;
import cn.wolfcode.luowowo.common.query.QueryObject;
import cn.wolfcode.luowowo.common.query.TravelQueryObject;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.Service;
import org.omg.CORBA.TCKind;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service
public class TravelServiceImpl implements ITravelService {

    @Autowired
    private TravelMapper travelMapper;
    @Autowired
    private TravelContentMapper contentMapper;

    @Override
    public void deleteByPrimaryKey(Long id) {

    }

    @Override
    public void insert(Travel record) {
        travelMapper.insert(record);
    }

    @Override
    public Travel selectByPrimaryKey(Long id) {
        Travel travel = travelMapper.selectByPrimaryKey(id);
        //通过游记id  查询他内容
        TravelContent travelContent = travelMapper.selectTravelContent(travel.getId());
        if(travelContent!=null){
            travel.setTravelContent(travelContent);
        }
        return travel;
    }

    @Override
    public List<Travel> selectAll() {
        return travelMapper.selectAll();
    }

    @Override
    public TravelContent selectTravelContent(Long travelId){
        return  travelMapper.selectTravelContent(travelId);
    }

    @Override
    public PageInfo<Travel> queryForList(TravelQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getType());
        List<Travel> list = travelMapper.queryForList(qo);
        return new PageInfo(list);
    }

    @Override//游记添加获取修改
    public AjaxResult saveOrUpdate(Travel entity) {
        AjaxResult result = new AjaxResult();
        //模拟填充载要
        entity.setSummary("AI 生成");
        //处理文章内容
        TravelContent content = entity.getTravelContent();
        //删除原关系
        contentMapper.deleteByPrimaryKey(entity.getId());
        if(entity.getId()==null){
            //新增
            travelMapper.insert(entity);
            //同步保存
            System.out.println(entity.getId());
            content.setId(entity.getId());
            contentMapper.insert(content);
        }else{
            //编辑
            travelMapper.updateByPrimaryKey(entity);
            //同步跟新
            content.setId(entity.getId());
            contentMapper.insert(content);
        }
        result.setData(entity.getId());
        return  result;
    }
    @Override//查询点击量前3 的游记
    public List<Travel> listViewnumTop3(Long destId) {
        TravelQueryObject qo = new TravelQueryObject();
        qo.setDestId(destId);
        return travelMapper.queryForList(qo);
    }


    @Override
    public void updateByPrimaryKey(Travel record) {
        travelMapper.updateByPrimaryKey(record);
    }
}
